#include "CWorldManager.h"
#include "SGD Wrappers/CSGD_TextureManager.h"

CWorldManager::CWorldManager( void )
{
	m_nMapSizeH = -1;
	m_nMapSizeW = -1;
	m_nTileSizeW = -1;
	m_nTileSizeH = -1;
	m_nTileWidth	= 32;
	m_nTileHeight	= 32;

}

CWorldManager::~CWorldManager( void )
{
	for (int i = 0; i < m_nMapSizeW; ++i)
	{
		delete[] TilesMap[i];
	}
	delete[] TilesMap;
}

CWorldManager* CWorldManager::GetInstance( void )
{
	static CWorldManager instance;
	return &instance;
}

void CWorldManager::LoadStage( const char * fileToLoad )
{
	TiXmlDocument doc;

	if(doc.LoadFile(fileToLoad) == false)
		return;

	TiXmlElement * pRoot = doc.RootElement();

	if(pRoot == NULL)
		return;
	int nCount = 0;

	

	pRoot->Attribute("Width",&m_nMapSizeW);
	pRoot->Attribute("Height",&m_nMapSizeH);

	TiXmlElement * ptSize = pRoot->FirstChildElement("TileSize");

	ptSize->Attribute("Width",&m_nTileSizeW);
	ptSize->Attribute("Height",&m_nTileSizeH);

	TilesMap = new Tiles*[m_nMapSizeH];
	for (int i = 0; i < m_nMapSizeW; ++i)
	{
		TilesMap[i] = new Tiles[m_nMapSizeW];

	}
	
	TiXmlElement * pImages	= pRoot->FirstChildElement("Images");
	if (pImages)
	{
		TiXmlElement * pImage = pImages->FirstChildElement("Image");
		char Buffer[256] = "resources//graphics//";
		strcat(Buffer,pImage->GetText());
		while (pImage)
		{
			nCount = CSGD_TextureManager::GetInstance()->LoadTexture(Buffer);
			ImageID.push_back(nCount);
			pImage = pImage->NextSiblingElement("Image");
		}
	}

	TiXmlElement * pTiles	= pRoot->FirstChildElement("Tiles");
	
	
	
	
	
	
	int tmpX = -1;
	int tmpY = -1;
	int tmpIndex = -1;


	TiXmlElement* pTile = pTiles->FirstChildElement("Tile");
	for (int i = 0; i < m_nMapSizeH; ++i)
	{
		for (int j = 0; j < m_nMapSizeW; ++j)
		{
			pTile->Attribute("tileX",&tmpX);
			TilesMap[i][j].tileX = tmpX;
			pTile->Attribute("tileY",&tmpY);
			TilesMap[i][j].tileY = tmpY;
			pTile->Attribute("tileIndex",&tmpIndex);
			TilesMap[i][j].ImageIDIndex = tmpIndex;
			
			pTile = pTile->NextSiblingElement();
		}
	}

}

void CWorldManager::Render()
{
	RECT sourceRect;
	
	for (int i = 0; i < m_nMapSizeH; ++i)
	{
		for (int j = 0; j < m_nMapSizeW; ++j)
		{
			sourceRect.left = (TilesMap[i][j].tileX * m_nTileWidth);
			sourceRect.top	= (TilesMap[i][j].tileY * m_nTileHeight);
			sourceRect.bottom = sourceRect.top + m_nTileHeight;
			sourceRect.right  = sourceRect.left + m_nTileWidth;
//<<<<<<< .mine
			if((j*m_nTileSizeH <= 500)&&(i*m_nTileSizeW <= 800))
				CSGD_TextureManager::GetInstance()->Draw(ImageID[TilesMap[i][j].ImageIDIndex],m_nTileSizeW*i,m_nTileSizeH*j,1,1,&sourceRect);
//=======
			//CSGD_TextureManager::GetInstance()->Draw(ImageID[TilesMap[i][j].ImageIDIndex],m_nMapSizeW*i,m_nMapSizeH*j,1,1,&sourceRect);
			if ((j * m_nTileHeight <= 500) && (i * m_nTileWidth <= 800) )
			CSGD_TextureManager::GetInstance()->Draw(ImageID[TilesMap[i][j].ImageIDIndex], m_nTileWidth*i, m_nTileHeight*j, 1.0f, 1.0f, &sourceRect);
//>>>>>>> .r37
		}
	}
}
